package com.hqyj.springBoot.modules.account.service.impl;

import com.hqyj.springBoot.modules.common.vo.Result;
import com.hqyj.springBoot.modules.account.dao.RoleDao;
import com.hqyj.springBoot.modules.account.entity.Role;
import com.hqyj.springBoot.modules.account.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleDao roleDao;

    @Override
    @Transactional
    public Result<Role> insertRole(Role role) {

        Role temp = roleDao.selectRoleByRoleName(role.getRoleName());
        if (temp != null){
            return new Result<>(Result.ResultStatus.FAILED.code, "Role is repeat");
        }

        role.setCreateDate(new Date());

        roleDao.insertRole(role);

        return new Result<>(Result.ResultStatus.SUCCESS.code, "Insert Role success",role);
    }

    @Override
    @Transactional
    public Result<Role> updateRole(Role role) {
        Role temp = roleDao.selectRoleByRoleName(role.getRoleName());
        if (temp != null && temp.getRoleId() == role.getRoleId()){
            return new Result<>(Result.ResultStatus.FAILED.code, "RoleName is repeat");
        }
        roleDao.updateRole(role);
        return new Result<>(Result.ResultStatus.SUCCESS.code, "Update role success",role);
    }

    @Override
    @Transactional
    public Result<Object> deleteRoleById(int roleId) {
        roleDao.deleteRoleById(roleId);
        return new Result<>(Result.ResultStatus.SUCCESS.code, "Delete Role success");
    }

    @Override
    @Transactional
    public Role selectRoleById(int roleId) {
        return roleDao.selectRoleById(roleId);
    }

    @Override
    public List<Role> getAllRolls() {
        return roleDao.getAllRoles();
    }
}
